(function($) {
    var player;
    $(document).ready(function() {
        //Background Video
        initHeaderVideo();
        
        $(".traipler-logo,.btn_play").one("click", function() {
            ga('send', 'event', 'Videos', 'Play ', "Video Promo", 1);
            $(".traipler-logo,.btn_play").fadeOut(500, function() {
                $('#vimeo0').show();
                // player.api('play');
            });
            return false;
        }).click();
        
        //Ricerca per tipologia
        $("#search_tipologia").on("submit",function(e){
            
            get_traiplers_rest("tipologia");
            e.preventDefault();
            return false;
        })
        $("#search_term").on("submit",function(e){
            
            get_traiplers_rest("term");
            e.preventDefault();
            return false;
        })
        
    }); //end ready
    
    
    function initPlayer() {
        var iframe = $('#vimeo0')[0];
        player = $f(iframe),
        // When the player is ready, add listeners for pause, finish, and playProgress
        player.addEvent('ready', function() {
            console.log('VIMEO ready');
            // player.api('play');
            player.addEvent('pause', onPause);
            player.addEvent('finish', onFinish);
            player.addEvent('playProgress', onPlayProgress);
        })
    }

    function onPause(id) {
        console.log('paused');
    }

    function onFinish(id) {
        console.log('finished');
        ga('send', 'event', 'Videos', 'Complete ', "Video Promo", 1);
    }

    function onPlayProgress(data, id) {
        console.log(data.seconds + 's played');
    }

    function goToHomePage() {
        //setCookieWelcome();
        location.href = "/homepage";
    }

    function setCookieWelcome() {
        $.cookie('welcome_page', 1);
    }
    
    
    function initHeaderVideo(){
        //$('#bgvideo').videoBackground(GLOBALPATH.theme_url+"/videos/SharpGiganticEchidna.webm");
        if (Modernizr.video && !isMobile()) {
            HeaderVideo.init({
                 //container: $('#wrap_video'),
                 container: $('.header-video'),
                 header: $('.header-video--media'),
                // videoTrigger: $("#video-trigger"),
                videoUrl : GLOBALPATH.theme_url+"/videos/homepage_traipler",
                 autoPlayVideo: false
           });    
            
        }else{
            $('.header-video').remove();
        }
    }
    
    
    
    function get_traiplers_rest(type){
        
       
        $("#search-container h2").hide()
        $("#search-container").show()
        $("#search-loading").fadeIn();
        
        var dfr = jQuery.Deferred();
        
        theUrl = GLOBALPATH.site_url+"/api/traiplers_rest/all_traiplers/?rest=true"
        
        if(type == "term")
        theUrl += "&searchTerm="+ $(".keyword").val();
        
        if(type == "tipologia")
        theUrl += "&tipologia="+ $("#tipologia").val();
        
        $.ajax({
            url: theUrl,
            dataType: 'json'
        })
        .done(function(data) {
            console.log("success");
            var traiplers = data.message.traiplers
            console.table(traiplers)
            
            fillTraiplers(traiplers)
            dfr.resolve(traiplers);
        
        })
        .error(function(e){
           dfr.reject(e);
        })
        
        return dfr.promise();
    }

    function fillTraiplers(traiplers){
        var template = $("#traipler_old_block_tmpl").html();
        var html ="";
        $.each(traiplers, function(index, traipler) {
            html += Mustache.render(template, traipler);
        });
        
        $('html, body').animate({
           scrollTop: $('#search-result').position().top-200
        }, 1000); 
        
        $("#search-container h2").show()
        $("#search-loading").fadeOut();
        $('#search-result').html(html);
    }

    
   
})(jQuery);




